﻿<cfscript>
/** 
* ORM BookBill
* 
* @hint "hibernate persistenter t_book_bill. 教材出入库票据" 
* 
*/ 

component 
	displayname="public.service.orm.senate.BookBill" 
	output=false 
	persistent=true 
	selectBeforeUpdate=true dynamicInsert=true dynamicUpdate=true 
	cacheuse="Transactional" 
	entityname="BookBill" 
	table="t_book_bill"
	{

	property type="string" name="BillID" column="bbid" fieldtype="id" generator="assigned" hint="教材出入库票据流水号";

	property type="string" name="StockType" column="stock_type" hint="出库(O)/入库(I)";

	property type="numeric" name="BookType" column="book_type" hint="教材种类";
	property type="numeric" name="BookCount" column="book_count" hint="教材数量";

	property type="string" name="Target" column="target" hint="出入库单位或个人";
	property type="string" name="BillCreateTime" column="opertime" hint="出单时间";
	property type="string" name="BillStat" column="bbstat" hint="出库单状态";

	property name="Term" fieldtype="many-to-one" fkcolumn="term_id" cfc="Term"  hint="学期";
	property name="Teacher" fieldtype="many-to-one" fkcolumn="tch_id" cfc="Teacher"  hint="教师";
	
	/**
	* @hint "自定义主键规则"
	* 
	* @StockType "出库(O)/入库(I)"
	*/
	public string function getNextID( required string StockType ) output=false {
		
		/* 
			主键生成规则 CHAR(13)
			BB + 1位出入库识别码 [I/O] + 6位日期代码 [YYMMDD] + 4位流水号
		*/
		
		var nextID = "BB" & arguments.StockType & dateFormat(now(), "YYMMDD") & numberFormat(1, "0___");
		
		var checkArray = ORMExecuteQuery("select BillID from BookBill 
											where 
											StockType = :stockType and BillID like :condition
											order by BillID desc", {
																	stockType=arguments.StockType, 
																	condition="BB" & arguments.StockType & dateFormat(now(), "YYMMDD") & "____" }, { maxResults=1 });
		
		if (arrayLen(checkArray)) {
			/* 创建一个追加id */
			nextID = "BB" & arguments.StockType & dateFormat(now(), "YYMMDD") & numberFormat( right(checkArray[1], 4) + 1, "0___");
		}
		
		return nextID;
	}
	
}

</cfscript>